package erwiki.admin.internal;

import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;

import erwiki.ui.app.admin.views.UserProfile;
//import org.elwiki.admin.app.internal.bundle.AdminAppUiActivator;
//import org.elwiki.api.IApplicationSession;
//import org.elwiki.api.IUserManager;
//import org.elwiki.api.authorization.user.UserProfile;
//import org.elwiki.api.exceptions.NoSuchPrincipalException;
//import org.elwiki.api.exceptions.WikiSecurityException;

public class ProfileOperations {

	public static void removeUserProfile(String loginid) {
		/*TODO: сделать для новой E4.
		
		IApplicationSession appSession = AdminAppUiActivator.getDefault().getAppSession();
		IUserManager userMgr = appSession.getUserManager();

		// TODO: diagnose errors correctly (possibly via UI session).
		try {
			userMgr.getUserDatabase().deleteByLoginName(loginid);
			MessageDialog.openInformation(null, "Информация", "User profile " + loginid + " has been deleted.");
		} catch (NoSuchPrincipalException e) {
			MessageDialog.openInformation(null, "Информация", "User profile has already been removed.");
		} catch (WikiSecurityException e) {
			MessageDialog.openError(null, "Ошибка", "Security problem: " + e.getMessage());
		}
		*/
	}

	/**
	 * @param loginname
	 * @param fullname
	 * @param password
	 * @param password2
	 * @param email
	 */
	public static void createUserProfile(String loginname, String fullname, String password, String password2,
			String email) {
		/*TODO: сделать для новой E4.
		
		IApplicationSession appSession = AdminAppUiActivator.getDefault().getAppSession();
		IUserManager userMgr = appSession.getUserManager();
		UserProfile profile;

		if (password == null || password.length() == 0) {
			// TODO: diagnose error correctly (possibly via UI session).
			MessageDialog.openError(null, "Ошибка", "Wrong password.");
			return;
		}
		if (!password.equals(password2)) {
			// TODO: diagnose error correctly (possibly via UI session).
			MessageDialog.openError(null, "Ошибка", "Passwords do not match!");
			return;
		}

		// Create new user
		profile = userMgr.getUserDatabase().newProfile();
		profile.setCreated(new Date());

		profile.setLoginName(loginname);
		profile.setFullname(fullname);
		profile.setPassword(password);
		profile.setEmail(email);

		try {
			userMgr.getUserDatabase().saveProfile(profile);
		} catch (WikiSecurityException e) {
			// TODO: diagnose error correctly (possibly via UI session).
			MessageDialog.openError(null, "Ошибка", "Unable to save " + e.getMessage());
		}

		// TODO: make message info: "User profile has been updated"
		 */
	}

	public static UserProfile getUserProfile(String loginid) {
		UserProfile profile = null;
		/*TODO: сделать для новой E4.
		IApplicationSession appSession = AdminAppUiActivator.getDefault().getAppSession();
		IUserManager userMgr = appSession.getUserManager();
		try {
			profile = userMgr.getUserDatabase().findByLoginName(loginid);
		} catch (NoSuchPrincipalException e) {
			// TODO: diagnose error correctly (possibly via UI session).
			//session.addMessage("I could not find user profile "+loginid);
		}
		*/
		return profile;
	}

}
